home *** CD-ROM | disk | FTP | other *** search
/ BCI NET 2 / BCI NET 2.iso / archives / programming / source / a2.0bemacs-src.lha / Emacs-19.25 / lisp / compile.el < prev    next >
Encoding:
Text File  |  1994-09-04  |  50.5 KB  |  1,329 lines

  1. ;;; compile.el --- run compiler as inferior of Emacs, parse error messages.
  2.  
  3. ;; Copyright (C) 1985, 86, 87, 93, 94 Free Software Foundation, Inc.
  4.  
  5. ;; Author: Roland McGrath <roland@prep.ai.mit.edu>
  6. ;; Maintainer: FSF
  7. ;; Keywords: tools, processes
  8.  
  9. ;; This file is part of GNU Emacs.
  10.  
  11. ;; GNU Emacs is free software; you can redistribute it and/or modify
  12. ;; it under the terms of the GNU General Public License as published by
  13. ;; the Free Software Foundation; either version 2, or (at your option)
  14. ;; any later version.
  15.  
  16. ;; GNU Emacs is distributed in the hope that it will be useful,
  17. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  19. ;; GNU General Public License for more details.
  20.  
  21. ;; You should have received a copy of the GNU General Public License
  22. ;; along with GNU Emacs; see the file COPYING.  If not, write to
  23. ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  24.  
  25. ;;; Commentary:
  26.  
  27. ;; This package provides the compile and grep facilities documented in
  28. ;; the Emacs user's manual.
  29.  
  30. ;;; Code:
  31.  
  32. ;;;###autoload
  33. (defvar compilation-mode-hook nil
  34.   "*List of hook functions run by `compilation-mode' (see `run-hooks').")
  35.  
  36. ;;;###autoload
  37. (defconst compilation-window-height nil
  38.   "*Number of lines in a compilation window.  If nil, use Emacs default.")
  39.  
  40. (defvar compilation-error-list nil
  41.   "List of error message descriptors for visiting erring functions.
  42. Each error descriptor is a cons (or nil).  Its car is a marker pointing to
  43. an error message.  If its cdr is a marker, it points to the text of the
  44. line the message is about.  If its cdr is a cons, it is a list
  45. \(\(DIRECTORY . FILE\) LINE [COLUMN]\).  Or its cdr may be nil if that
  46. error is not interesting.
  47.  
  48. The value may be t instead of a list; this means that the buffer of
  49. error messages should be reparsed the next time the list of errors is wanted.
  50.  
  51. Some other commands (like `diff') use this list to control the error
  52. message tracking facilites; if you change its structure, you should make
  53. sure you also change those packages.  Perhaps it is better not to change
  54. it at all.")
  55.  
  56. (defvar compilation-old-error-list nil
  57.   "Value of `compilation-error-list' after errors were parsed.")
  58.  
  59. (defvar compilation-parse-errors-function 'compilation-parse-errors 
  60.   "Function to call to parse error messages from a compilation.
  61. It takes args LIMIT-SEARCH and FIND-AT-LEAST.
  62. If LIMIT-SEARCH is non-nil, don't bother parsing past that location.
  63. If FIND-AT-LEAST is non-nil, don't bother parsing after finding that 
  64. many new errors.
  65. It should read in the source files which have errors and set
  66. `compilation-error-list' to a list with an element for each error message
  67. found.  See that variable for more info.")
  68.  
  69. ;;;###autoload
  70. (defvar compilation-buffer-name-function nil
  71.   "Function to compute the name of a compilation buffer.
  72. The function receives one argument, the name of the major mode of the
  73. compilation buffer.  It should return a string.
  74. nil means compute the name with `(concat \"*\" (downcase major-mode) \"*\")'.")
  75.  
  76. ;;;###autoload
  77. (defvar compilation-finish-function nil
  78.   "*Function to call when a compilation process finishes.
  79. It is called with two arguments: the compilation buffer, and a string
  80. describing how the process finished.")
  81.  
  82. (defvar compilation-last-buffer nil
  83.   "The most recent compilation buffer.
  84. A buffer becomes most recent when its compilation is started
  85. or when it is used with \\[next-error] or \\[compile-goto-error].")
  86.  
  87. (defvar compilation-in-progress nil
  88.   "List of compilation processes now running.")
  89. (or (assq 'compilation-in-progress minor-mode-alist)
  90.     (setq minor-mode-alist (cons '(compilation-in-progress " Compiling")
  91.                  minor-mode-alist)))
  92.  
  93. (defvar compilation-parsing-end nil
  94.   "Position of end of buffer when last error messages were parsed.")
  95.  
  96. (defvar compilation-error-message "No more errors"
  97.   "Message to print when no more matches are found.")
  98.  
  99. (defvar compilation-num-errors-found)
  100.  
  101. (defvar compilation-error-regexp-alist
  102.   '(
  103.     ;; NOTE!  This first one is repeated in grep-regexp-alist, below.
  104.  
  105.     ;; 4.3BSD grep, cc, lint pass 1:
  106.     ;;     /usr/src/foo/foo.c(8): warning: w may be used before set
  107.     ;; or GNU utilities:
  108.     ;;     foo.c:8: error message
  109.     ;; or HP-UX 7.0 fc:
  110.     ;;     foo.f          :16    some horrible error message
  111.     ;;
  112.     ;; We'll insist that the number be followed by a colon or closing
  113.     ;; paren, because otherwise this matches just about anything
  114.     ;; containing a number with spaces around it.
  115.     ("\n\\([^:( \t\n]+\\)[:(][ \t]*\\([0-9]+\\)[:) \t]" 1 2)
  116.  
  117.     ;; 4.3BSD lint pass 2
  118.     ;;     strcmp: variable # of args. llib-lc(359)  ::  /usr/src/foo/foo.c(8)
  119.     ("[ \t:]\\([^:( \t\n]+\\)[:(](+[ \t]*\\([0-9]+\\))[:) \t]*$" 1 2)
  120.  
  121.     ;; 4.3BSD lint pass 3
  122.     ;;     bloofle defined( /users/wolfgang/foo.c(4) ), but never used
  123.     ;; This used to be
  124.     ;; ("[ \t(]+\\([^:( \t\n]+\\)[:( \t]+\\([0-9]+\\)[:) \t]+" 1 2)
  125.     ;; which is regexp Impressionism - it matches almost anything!
  126.     ("([ \t]*\\([^:( \t\n]+\\)[:(][ \t]*\\([0-9]+\\))" 1 2)
  127.  
  128.     ;; Ultrix 3.0 f77:
  129.     ;;  fort: Severe: addstf.f, line 82: Missing operator or delimiter symbol
  130.     ("\nfort: [^:\n]*: \\([^ \n]*\\), line \\([0-9]+\\):" 1 2)
  131.     ;;  Error on line 3 of t.f: Execution error unclassifiable statement    
  132.     ;; Unknown who does this:
  133.     ;;  Line 45 of "foo.c": bloofel undefined
  134.     ;; Absoft FORTRAN 77 Compiler 3.1.3
  135.     ;;  error on line 19 of fplot.f: spelling error?
  136.     ;;  warning on line 17 of fplot.f: data type is undefined for variable d
  137.     ("\\(\n\\|on \\)[Ll]ine[ \t]+\\([0-9]+\\)[ \t]+\
  138. of[ \t]+\"?\\([^\":\n]+\\)\"?:" 3 2)
  139.  
  140.     ;; Apollo cc, 4.3BSD fc:
  141.     ;;    "foo.f", line 3: Error: syntax error near end of statement
  142.     ;; IBM RS6000:
  143.     ;;  "vvouch.c", line 19.5: 1506-046 (S) Syntax error.
  144.     ;; Unknown compiler:
  145.     ;;  File "foobar.ml", lines 5-8, characters 20-155: blah blah
  146.     ;; Microtec mcc68k:
  147.     ;;  "foo.c", line 32 pos 1; (E) syntax error; unexpected symbol: "lossage"
  148.     ("\"\\([^,\" \n\t]+\\)\", lines? \\([0-9]+\\)[:., -]" 1 2)
  149.  
  150.     ;; MIPS RISC CC - the one distributed with Ultrix:
  151.     ;;    ccom: Error: foo.c, line 2: syntax error
  152.     ;; DEC AXP OSF/1 cc
  153.     ;;  /usr/lib/cmplrs/cc/cfe: Error: foo.c: 1: blah blah 
  154.     ("rror: \\([^,\" \n\t]+\\)[,:] \\(line \\)?\\([0-9]+\\):" 1 3)
  155.  
  156.     ;; IBM AIX PS/2 C version 1.1:
  157.     ;;    ****** Error number 140 in line 8 of file errors.c ******
  158.     ("in line \\([0-9]+\\) of file \\([^ \n]+[^. \n]\\)\\.? " 2 1)
  159.     ;; IBM AIX lint is too painful to do right this way.  File name
  160.     ;; prefixes entire sections rather than being on each line.
  161.  
  162.     ;; Lucid Compiler, lcc 3.x
  163.     ;; E, file.cc(35,52) Illegal operation on pointers
  164.     ("\n[EW], \\([^(\n]*\\)(\\([0-9]+\\),[ \t]*\\([0-9]+\\)" 1 2 3)
  165.  
  166.     ;; Compiler Toolkit cocktail
  167.     ;; "ctla.pars", 151, 38: Error       illegal character: ;
  168.     ;; "ctla.pars", 167,  1: Warning     node type not used: ModuleName
  169.     ;; CHFIXME: find out how to "or" functions
  170.     ("\n\\\"\\([^ \n\\\"]+\\)\\\",[ ]*\\([0-9]+\\),[ ]*\\([0-9]+\\): Error" 1 2 3)
  171.     ("\n\\\"\\([^ \n\\\"]+\\)\\\",[ ]*\\([0-9]+\\),[ ]*\\([0-9]+\\): Warning" 1 2 3)
  172.  
  173.     ;; Amigados SAS/C Compiler 6.3
  174.     ;; amiga_dump.c 53 Error 72: conflict with previous declaration
  175.     ;; amiga_dump.c 164 Warning 72: conflict with previous declaration
  176.     ;; CHFIXME: find out how to "or" functions
  177.     ("\n\\([^ \n]+\\) \\([0-9]+\\) Warning" 1 2)
  178.     ("\n\\([^ \n]+\\) \\([0-9]+\\) Error" 1 2)
  179.     )
  180.   "Alist that specifies how to match errors in compiler output.
  181. Each element has the form (REGEXP FILE-IDX LINE-IDX [COLUMN-IDX]).
  182. If REGEXP matches, the FILE-IDX'th subexpression gives the file name, and
  183. the LINE-IDX'th subexpression gives the line number.  If COLUMN-IDX is
  184. given, the COLUMN-IDX'th subexpression gives the column number on that line.")
  185.  
  186. (defvar compilation-read-command t
  187.   "If not nil, M-x compile reads the compilation command to use.
  188. Otherwise, M-x compile just uses the value of `compile-command'.")
  189.  
  190. (defvar compilation-ask-about-save t
  191.   "If not nil, M-x compile asks which buffers to save before compiling.
  192. Otherwise, it saves all modified buffers without asking.")
  193.  
  194. (defvar grep-regexp-alist
  195.   '(("^\\([^:( \t\n]+\\)[:( \t]+\\([0-9]+\\)[:) \t]" 1 2))
  196.   "Regexp used to match grep hits.  See `compilation-error-regexp-alist'.")
  197.  
  198. (defvar grep-command "grep -n "
  199.   "Last grep command used in \\{grep}; default for next grep.")
  200.  
  201. ;;;###autoload
  202. (defvar compilation-search-path '(nil)
  203.   "*List of directories to search for source files named in error messages.
  204. Elements should be directory names, not file names of directories.
  205. nil as an element means to try the default directory.")
  206.  
  207. (defvar compile-command "make -k "
  208.   "Last shell command used to do a compilation; default for next compilation.
  209.  
  210. Sometimes it is useful for files to supply local values for this variable.
  211. You might also use mode hooks to specify it in certain modes, like this:
  212.  
  213.     (setq c-mode-hook
  214.       '(lambda () (or (file-exists-p \"makefile\") (file-exists-p \"Makefile\")
  215.               (progn (make-local-variable 'compile-command)
  216.                  (setq compile-command
  217.                     (concat \"make -k \"
  218.                         buffer-file-name))))))")
  219.  
  220. (defconst compilation-enter-directory-regexp
  221.   ": Entering directory `\\(.*\\)'$"
  222.   "Regular expression matching lines that indicate a new current directory.
  223. This must contain one \\(, \\) pair around the directory name.
  224.  
  225. The default value matches lines printed by the `-w' option of GNU Make.")
  226.  
  227. (defconst compilation-leave-directory-regexp
  228.   ": Leaving directory `\\(.*\\)'$"
  229.   "Regular expression matching lines that indicate restoring current directory.
  230. This may contain one \\(, \\) pair around the name of the directory
  231. being moved from.  If it does not, the last directory entered \(by a
  232. line matching `compilation-enter-directory-regexp'\) is assumed.
  233.  
  234. The default value matches lines printed by the `-w' option of GNU Make.")
  235.  
  236. (defvar compilation-directory-stack nil
  237.   "Stack of previous directories for `compilation-leave-directory-regexp'.
  238. The head element is the directory the compilation was started in.")
  239.  
  240. ;; History of compile commands.
  241. (defvar compile-history nil)
  242. ;; History of grep commands.
  243. (defvar grep-history nil)
  244.  
  245. ;;;###autoload
  246. (defun compile (command)
  247.   "Compile the program including the current buffer.  Default: run `make'.
  248. Runs COMMAND, a shell command, in a separate process asynchronously
  249. with output going to the buffer `*compilation*'.
  250.  
  251. You can then use the command \\[next-error] to find the next error message
  252. and move to the source code that caused it.
  253.  
  254. To run more than one compilation at once, start one and rename the
  255. \`*compilation*' buffer to some other name with \\[rename-buffer].
  256. Then start the next one.
  257.  
  258. The name used for the buffer is actually whatever is returned by
  259. the function in `compilation-buffer-name-function', so you can set that
  260. to a function that generates a unique name."
  261.   (interactive
  262.    (if compilation-read-command
  263.        (list (read-from-minibuffer "Compile command: "
  264.                                  compile-command nil nil
  265.                                  '(compile-history . 1)))
  266.      (list compile-command)))
  267.   (setq compile-command command)
  268.   (save-some-buffers (not compilation-ask-about-save) nil)
  269.   (compile-internal compile-command "No more errors"))
  270.  
  271. ;;;###autoload
  272. (defun grep (command-args)
  273.   "Run grep, with user-specified args, and collect output in a buffer.
  274. While grep runs asynchronously, you can use the \\[next-error] command
  275. to find the text that grep hits refer to.
  276.  
  277. This command uses a special history list for its arguments, so you can
  278. easily repeat a grep command."
  279.   (interactive
  280.    (list (read-from-minibuffer "Run grep (like this): "
  281.                    grep-command nil nil 'grep-history)))
  282.   (compile-internal (concat command-args 
  283.                 (if (eq system-type 'amigados) " nil:"
  284.                   " /dev/null"))
  285.             "No more grep hits" "grep"
  286.             ;; Give it a simpler regexp to match.
  287.             nil grep-regexp-alist))
  288.  
  289. (defun compile-internal (command error-message
  290.                  &optional name-of-mode parser regexp-alist
  291.                  name-function)
  292.   "Run compilation command COMMAND (low level interface).
  293. ERROR-MESSAGE is a string to print if the user asks to see another error
  294. and there are no more errors.  Third argument NAME-OF-MODE is the name
  295. to display as the major mode in the compilation buffer.
  296.  
  297. Fourth arg PARSER is the error parser function (nil means the default).  Fifth
  298. arg REGEXP-ALIST is the error message regexp alist to use (nil means the
  299. default).  Sixth arg NAME-FUNCTION is a function called to name the buffer (nil
  300. means the default).  The defaults for these variables are the global values of
  301. \`compilation-parse-errors-function', `compilation-error-regexp-alist', and
  302. \`compilation-buffer-name-function', respectively.
  303.  
  304. Returns the compilation buffer created."
  305.   (let (outbuf)
  306.     (save-excursion
  307.       (or name-of-mode
  308.       (setq name-of-mode "Compilation"))
  309.       (setq outbuf
  310.         (get-buffer-create
  311.          (funcall (or name-function compilation-buffer-name-function
  312.               (function (lambda (mode)
  313.                       (concat "*" (downcase mode) "*"))))
  314.               name-of-mode)))
  315.       (set-buffer outbuf)
  316.       (let ((comp-proc (get-buffer-process (current-buffer))))
  317.     (if comp-proc
  318.         (if (or (not (eq (process-status comp-proc) 'run))
  319.             (yes-or-no-p
  320.              (format "A %s process is running; kill it? "
  321.                  name-of-mode)))
  322.         (condition-case ()
  323.             (progn
  324.               (interrupt-process comp-proc)
  325.               (sit-for 1)
  326.               (delete-process comp-proc))
  327.           (error nil))
  328.           (error "Cannot have two processes in `%s' at once"
  329.              (buffer-name))
  330.           )))
  331.       ;; In case the compilation buffer is current, make sure we get the global
  332.       ;; values of compilation-error-regexp-alist, etc.
  333.       (kill-all-local-variables))
  334.     (let ((regexp-alist (or regexp-alist compilation-error-regexp-alist))
  335.       (parser (or parser compilation-parse-errors-function))
  336.       (thisdir default-directory)
  337.       outwin) 
  338.       (save-excursion
  339.     ;; Clear out the compilation buffer and make it writable.
  340.     ;; Change its default-directory to the directory where the compilation
  341.     ;; will happen, and insert a `cd' command to indicate this.
  342.     (set-buffer outbuf)
  343.     (setq buffer-read-only nil)
  344.     (erase-buffer)
  345.     (setq default-directory thisdir)
  346.     (insert "cd " thisdir "\n" command "\n")
  347.     (set-buffer-modified-p nil))
  348.       ;; If we're already in the compilation buffer, go to the end
  349.       ;; of the buffer, so point will track the compilation output.
  350.       (if (eq outbuf (current-buffer))
  351.       (goto-char (point-max)))
  352.       ;; Pop up the compilation buffer.
  353.       (setq outwin (display-buffer outbuf))
  354.       (save-excursion
  355.     (set-buffer outbuf)
  356.     (compilation-mode)
  357.     (buffer-disable-undo (current-buffer))
  358.     ;; (setq buffer-read-only t)  ;;; Non-ergonomic.
  359.     (set (make-local-variable 'compilation-parse-errors-function) parser)
  360.     (set (make-local-variable 'compilation-error-message) error-message)
  361.     (set (make-local-variable 'compilation-error-regexp-alist) regexp-alist)
  362.     (setq default-directory thisdir
  363.           compilation-directory-stack (list default-directory))
  364.     (set-window-start outwin (point-min))
  365.     (setq mode-name name-of-mode)
  366.     (or (eq outwin (selected-window))
  367.         (set-window-point outwin (point-min)))
  368.     (and compilation-window-height
  369.          (= (window-width outwin) (frame-width))
  370.          (let ((w (selected-window)))
  371.            (unwind-protect
  372.            (progn
  373.              (select-window outwin)
  374.              (enlarge-window (- compilation-window-height
  375.                     (window-height))))
  376.          (select-window w))))
  377.     ;; Start the compilation.
  378.     (if (fboundp 'start-process)
  379.         (let ((proc (start-process-shell-command (downcase mode-name)
  380.                              outbuf
  381.                              command)))
  382.           (set-process-sentinel proc 'compilation-sentinel)
  383.           (set-process-filter proc 'compilation-filter)
  384.           (set-marker (process-mark proc) (point) outbuf)
  385.           (setq compilation-in-progress 
  386.             (cons proc compilation-in-progress)))
  387.       ;; No asynchronous processes available
  388.       (message (format "Executing `%s'..." command))
  389.       (let ((status (call-process shell-file-name nil outbuf nil "-c"
  390.                       command))))
  391.       (message (format "Executing `%s'...done" command)))))
  392.     ;; Make it so the next C-x ` will use this buffer.
  393.     (setq compilation-last-buffer outbuf)))
  394.  
  395. (defvar compilation-minor-mode-map
  396.   (let ((map (make-sparse-keymap)))
  397.     (define-key map [mouse-2] 'compile-mouse-goto-error)
  398.     (define-key map "\C-c\C-c" 'compile-goto-error)
  399.     (define-key map "\C-c\C-k" 'kill-compilation)
  400.     (define-key map "\M-n" 'compilation-next-error)
  401.     (define-key map "\M-p" 'compilation-previous-error)
  402.     (define-key map "\M-{" 'compilation-previous-file)
  403.     (define-key map "\M-}" 'compilation-next-file)
  404.     map)
  405.   "Keymap for `compilation-minor-mode'.")
  406.  
  407. (defvar compilation-mode-map
  408.   (let ((map (cons 'keymap compilation-minor-mode-map)))
  409.     (define-key map " " 'scroll-up)
  410.     (define-key map "\^?" 'scroll-down)
  411.     map)
  412.   "Keymap for compilation log buffers.
  413. `compilation-minor-mode-map' is a cdr of this.")
  414.  
  415. (defun compilation-mode ()
  416.   "Major mode for compilation log buffers.
  417. \\<compilation-mode-map>To visit the source for a line-numbered error,
  418. move point to the error message line and type \\[compile-goto-error].
  419. To kill the compilation, type \\[kill-compilation].
  420.  
  421. Runs `compilation-mode-hook' with `run-hooks' (which see)."
  422.   (interactive)
  423.   (fundamental-mode)
  424.   (use-local-map compilation-mode-map)
  425.   (setq major-mode 'compilation-mode
  426.     mode-name "Compilation")
  427.   (compilation-setup)
  428.   (run-hooks 'compilation-mode-hook))
  429.  
  430. ;; Prepare the buffer for the compilation parsing commands to work.
  431. (defun compilation-setup ()
  432.   ;; Make the buffer's mode line show process state.
  433.   (setq mode-line-process '(":%s"))
  434.   (set (make-local-variable 'compilation-error-list) nil)
  435.   (set (make-local-variable 'compilation-old-error-list) nil)
  436.   (set (make-local-variable 'compilation-parsing-end) 1)
  437.   (set (make-local-variable 'compilation-directory-stack) nil)
  438.   (setq compilation-last-buffer (current-buffer)))
  439.  
  440. (defvar compilation-minor-mode nil
  441.   "Non-nil when in compilation-minor-mode.
  442. In this minor mode, all the error-parsing commands of the
  443. Compilation major mode are available.")
  444. (make-variable-buffer-local 'compilation-minor-mode)
  445.  
  446. (or (assq 'compilation-minor-mode minor-mode-alist)
  447.     (setq minor-mode-alist (cons '(compilation-minor-mode " Compilation")
  448.                  minor-mode-alist)))
  449. (or (assq 'compilation-minor-mode minor-mode-map-alist)
  450.     (setq minor-mode-map-alist (cons (cons 'compilation-minor-mode
  451.                        compilation-minor-mode-map)
  452.                      minor-mode-map-alist)))
  453.  
  454. ;;;###autoload
  455. (defun compilation-minor-mode (&optional arg)
  456.   "Toggle compilation minor mode.
  457. With arg, turn compilation mode on if and only if arg is positive.
  458. See `compilation-mode'."
  459.   (interactive "P")
  460.   (if (setq compilation-minor-mode (if (null arg)
  461.                        (null compilation-minor-mode)
  462.                      (> (prefix-numeric-value arg) 0)))
  463.       (compilation-setup)))
  464.  
  465. ;; Called when compilation process changes state.
  466. (defun compilation-sentinel (proc msg)
  467.   "Sentinel for compilation buffers."
  468.   (let ((buffer (process-buffer proc)))
  469.     (if (memq (process-status proc) '(signal exit))
  470.     (progn
  471.       (if (null (buffer-name buffer))
  472.           ;; buffer killed
  473.           (set-process-buffer proc nil)
  474.         (let ((obuf (current-buffer))
  475.           omax opoint)
  476.           ;; save-excursion isn't the right thing if
  477.           ;; process-buffer is current-buffer
  478.           (unwind-protect
  479.           (progn
  480.             ;; Write something in the compilation buffer
  481.             ;; and hack its mode line.
  482.             (set-buffer buffer)
  483.             (let ((buffer-read-only nil))
  484.               (setq omax (point-max)
  485.                 opoint (point))
  486.               (goto-char omax)
  487.               ;; Record where we put the message, so we can ignore it
  488.               ;; later on.
  489.               (insert ?\n mode-name " " msg)
  490.               (forward-char -1)
  491.               (insert " at " (substring (current-time-string) 0 19))
  492.               (forward-char 1)
  493.               (setq mode-line-process
  494.                 (concat ":"
  495.                     (symbol-name (process-status proc))))
  496.               ;; Since the buffer and mode line will show that the
  497.               ;; process is dead, we can delete it now.  Otherwise it
  498.               ;; will stay around until M-x list-processes.
  499.               (delete-process proc)
  500.               ;; Force mode line redisplay soon.
  501.               (set-buffer-modified-p (buffer-modified-p)))
  502.             (if (and opoint (< opoint omax))
  503.             (goto-char opoint))
  504.             (if compilation-finish-function
  505.             (funcall compilation-finish-function buffer msg)))
  506.         (set-buffer obuf))))
  507.       (setq compilation-in-progress (delq proc compilation-in-progress))
  508.       ))))
  509.  
  510. (defun compilation-filter (proc string)
  511.   "Process filter for compilation buffers.
  512. Just inserts the text, but uses `insert-before-markers'."
  513.   (save-excursion
  514.     (set-buffer (process-buffer proc))
  515.     (let ((buffer-read-only nil))
  516.       (save-excursion
  517.     (goto-char (process-mark proc))
  518.     (insert-before-markers string)
  519.     (set-marker (process-mark proc) (point))))))
  520.  
  521. ;; Return the cdr of compilation-old-error-list for the error containing point.
  522. (defun compile-error-at-point ()
  523.   (compile-reinitialize-errors nil (point))
  524.   (let ((errors compilation-old-error-list))
  525.     (while (and errors
  526.         (> (point) (car (car errors))))
  527.       (setq errors (cdr errors)))
  528.     errors))
  529.  
  530. (defsubst compilation-buffer-p (buffer)
  531.   (assq 'compilation-error-list (buffer-local-variables buffer)))
  532.  
  533. (defun compilation-next-error (n)
  534.   "Move point to the next error in the compilation buffer.
  535. Does NOT find the source line like \\[next-error]."
  536.   (interactive "p")
  537.   (or (compilation-buffer-p (current-buffer))
  538.       (error "Not in a compilation buffer."))
  539.   (setq compilation-last-buffer (current-buffer))
  540.  
  541.   (let ((errors (compile-error-at-point)))
  542.  
  543.     ;; Move to the error after the one containing point.
  544.     (goto-char (car (if (< n 0)
  545.             (let ((i 0)
  546.                   (e compilation-old-error-list))
  547.               ;; See how many cdrs away ERRORS is from the start.
  548.               (while (not (eq e errors))
  549.                 (setq i (1+ i)
  550.                   e (cdr e)))
  551.               (if (> (- n) i)
  552.                   (error "Moved back past first error")
  553.                 (nth (+ i n) compilation-old-error-list)))
  554.               (let ((compilation-error-list (cdr errors)))
  555.             (compile-reinitialize-errors nil nil n)
  556.             (if compilation-error-list
  557.                 (nth (1- n) compilation-error-list)
  558.               (error "Moved past last error"))))))))
  559.  
  560. (defun compilation-previous-error (n)
  561.   "Move point to the previous error in the compilation buffer.
  562. Does NOT find the source line like \\[next-error]."
  563.   (interactive "p")
  564.   (compilation-next-error (- n)))
  565.  
  566.  
  567. ;; Given an elt of `compilation-error-list', return an object representing
  568. ;; the referenced file which is equal to (but not necessarily eq to) what
  569. ;; this function would return for another error in the same file.
  570. (defsubst compilation-error-filedata (data)
  571.   (setq data (cdr data))
  572.   (if (markerp data)
  573.       (marker-buffer data)
  574.     (car data)))
  575.  
  576. ;; Return a string describing a value from compilation-error-filedata.
  577. ;; This value is not necessarily useful as a file name, but should be
  578. ;; indicative to the user of what file's errors are being referred to.
  579. (defsubst compilation-error-filedata-file-name (filedata)
  580.   (if (bufferp filedata)
  581.       (buffer-file-name filedata)
  582.     (car filedata)))
  583.  
  584. (defun compilation-next-file (n)
  585.   "Move point to the next error for a different file than the current one."
  586.   (interactive "p")
  587.   (or (compilation-buffer-p (current-buffer))
  588.       (error "Not in a compilation buffer."))
  589.   (setq compilation-last-buffer (current-buffer))
  590.  
  591.   (let ((reversed (< n 0))
  592.     errors filedata)
  593.  
  594.     (if (not reversed)
  595.     (setq errors (or (compile-error-at-point)
  596.              (error "Moved past last error")))
  597.  
  598.       ;; Get a reversed list of the errors up through the one containing point.
  599.       (compile-reinitialize-errors nil (point))
  600.       (setq errors (reverse compilation-old-error-list)
  601.         n (- n))
  602.  
  603.       ;; Ignore errors after point.  (car ERRORS) will be the error
  604.       ;; containing point, (cadr ERRORS) the one before it.
  605.       (while (and errors
  606.           (< (point) (car (car errors))))
  607.     (setq errors (cdr errors))))
  608.  
  609.     (while (> n 0)
  610.       (setq filedata (compilation-error-filedata (car errors)))
  611.  
  612.       ;; Skip past the following errors for this file.
  613.       (while (equal filedata
  614.             (compilation-error-filedata
  615.              (car (or errors
  616.                   (if reversed
  617.                   (error "%s the first erring file"
  618.                      (compilation-error-filedata-file-name
  619.                       filedata))
  620.                 (let ((compilation-error-list nil))
  621.                   ;; Parse some more.
  622.                   (compile-reinitialize-errors nil nil 2)
  623.                   (setq errors compilation-error-list)))
  624.                   (error "%s is the last erring file" 
  625.                      (compilation-error-filedata-file-name
  626.                       filedata))))))
  627.     (setq errors (cdr errors)))
  628.  
  629.       (setq n (1- n)))
  630.  
  631.     ;; Move to the following error.
  632.     (goto-char (car (car (or errors
  633.                  (if reversed
  634.                  (error "This is the first erring file")
  635.                    (let ((compilation-error-list nil))
  636.                  ;; Parse the last one.
  637.                  (compile-reinitialize-errors nil nil 1)
  638.                  compilation-error-list))))))))
  639.  
  640. (defun compilation-previous-file (n)
  641.   "Move point to the previous error for a different file than the current one."
  642.   (interactive "p")
  643.   (compilation-next-file (- n)))
  644.  
  645.  
  646. (defun kill-compilation ()
  647.   "Kill the process made by the \\[compile] command."
  648.   (interactive)
  649.   (let ((buffer (compilation-find-buffer)))
  650.     (if (get-buffer-process buffer)
  651.     (interrupt-process (get-buffer-process buffer))
  652.       (error "The compilation process is not running."))))
  653.  
  654.  
  655. ;; Parse any new errors in the compilation buffer,
  656. ;; or reparse from the beginning if the user has asked for that.
  657. (defun compile-reinitialize-errors (reparse
  658.                     &optional limit-search find-at-least)
  659.   (save-excursion
  660.     (set-buffer compilation-last-buffer)
  661.     ;; If we are out of errors, or if user says "reparse",
  662.     ;; discard the info we have, to force reparsing.
  663.     (if (or (eq compilation-error-list t)
  664.         reparse)
  665.     (compilation-forget-errors))
  666.     (if (and compilation-error-list
  667.          (or (not limit-search)
  668.          (> compilation-parsing-end limit-search))
  669.          (or (not find-at-least)
  670.          (>= (length compilation-error-list) find-at-least)))
  671.     ;; Since compilation-error-list is non-nil, it points to a specific
  672.     ;; error the user wanted.  So don't move it around.
  673.     nil
  674.       ;; This was here for a long time (before my rewrite); why? --roland
  675.       ;;(switch-to-buffer compilation-last-buffer)
  676.       (set-buffer-modified-p nil)
  677.       (if (< compilation-parsing-end (point-max))
  678.       ;; compilation-error-list might be non-nil if we have a non-nil
  679.       ;; LIMIT-SEARCH or FIND-AT-LEAST arg.  In that case its value
  680.       ;; records the current position in the error list, and we must
  681.       ;; preserve that after reparsing.
  682.       (let ((error-list-pos compilation-error-list))
  683.         (funcall compilation-parse-errors-function
  684.              limit-search
  685.              (and find-at-least
  686.               ;; We only need enough new parsed errors to reach
  687.               ;; FIND-AT-LEAST errors past the current
  688.               ;; position.
  689.               (- find-at-least (length compilation-error-list))))
  690.         ;; Remember the entire list for compilation-forget-errors.  If
  691.         ;; this is an incremental parse, append to previous list.  If
  692.         ;; we are parsing anew, compilation-forget-errors cleared
  693.         ;; compilation-old-error-list above.
  694.         (setq compilation-old-error-list
  695.           (nconc compilation-old-error-list compilation-error-list))
  696.         (if error-list-pos
  697.         ;; We started in the middle of an existing list of parsed
  698.         ;; errors before parsing more; restore that position.
  699.         (setq compilation-error-list error-list-pos))
  700.         )))))
  701.  
  702. (defun compile-mouse-goto-error (event)
  703.   (interactive "e")
  704.   (save-excursion
  705.     (set-buffer (window-buffer (posn-window (event-end event))))
  706.     (goto-char (posn-point (event-end event)))
  707.  
  708.     (or (compilation-buffer-p (current-buffer))
  709.     (error "Not in a compilation buffer."))
  710.     (setq compilation-last-buffer (current-buffer))
  711.     (compile-reinitialize-errors nil (point))
  712.  
  713.     ;; Move to bol; the marker for the error on this line will point there.
  714.     (beginning-of-line)
  715.  
  716.     ;; Move compilation-error-list to the elt of compilation-old-error-list
  717.     ;; we want.
  718.     (setq compilation-error-list compilation-old-error-list)
  719.     (while (and compilation-error-list
  720.         (> (point) (car (car compilation-error-list))))
  721.       (setq compilation-error-list (cdr compilation-error-list)))
  722.     (or compilation-error-list
  723.     (error "No error to go to")))
  724.   (select-window (posn-window (event-end event)))
  725.   ;; Move to another window, so that next-error's window changes
  726.   ;; result in the desired setup.
  727.   (or (one-window-p)
  728.       (progn
  729.     (other-window -1)
  730.     ;; other-window changed the selected buffer,
  731.     ;; but we didn't want to do that.
  732.     (set-buffer compilation-last-buffer)))
  733.  
  734.   (push-mark)
  735.   (next-error 1))
  736.  
  737. (defun compile-goto-error (&optional argp)
  738.   "Visit the source for the error message point is on.
  739. Use this command in a compilation log buffer.  Sets the mark at point there.
  740. \\[universal-argument] as a prefix arg means to reparse the buffer's error messages first;
  741. other kinds of prefix arguments are ignored."
  742.   (interactive "P")
  743.   (or (compilation-buffer-p (current-buffer))
  744.       (error "Not in a compilation buffer."))
  745.   (setq compilation-last-buffer (current-buffer))
  746.   (compile-reinitialize-errors (consp argp) (point))
  747.  
  748.   ;; Move to bol; the marker for the error on this line will point there.
  749.   (beginning-of-line)
  750.  
  751.   ;; Move compilation-error-list to the elt of compilation-old-error-list
  752.   ;; we want.
  753.   (setq compilation-error-list compilation-old-error-list)
  754.   (while (and compilation-error-list
  755.           (> (point) (car (car compilation-error-list))))
  756.     (setq compilation-error-list (cdr compilation-error-list)))
  757.  
  758.   ;; Move to another window, so that next-error's window changes
  759.   ;; result in the desired setup.
  760.   (or (one-window-p)
  761.       (progn
  762.     (other-window -1)
  763.     ;; other-window changed the selected buffer,
  764.     ;; but we didn't want to do that.
  765.     (set-buffer compilation-last-buffer)))
  766.  
  767.   (push-mark)
  768.   (next-error 1))
  769.  
  770. ;; Return a compilation buffer.
  771. ;; If the current buffer is a compilation buffer, return it.
  772. ;; If compilation-last-buffer is set to a live buffer, use that.
  773. ;; Otherwise, look for a compilation buffer and signal an error
  774. ;; if there are none.
  775. (defun compilation-find-buffer (&optional other-buffer)
  776.   (if (and (not other-buffer)
  777.        (compilation-buffer-p (current-buffer)))
  778.       ;; The current buffer is a compilation buffer.
  779.       (current-buffer)
  780.     (if (and compilation-last-buffer (buffer-name compilation-last-buffer)
  781.          (or (not other-buffer) (not (eq compilation-last-buffer
  782.                          (current-buffer)))))
  783.     compilation-last-buffer
  784.       (let ((buffers (buffer-list)))
  785.     (while (and buffers (or (not (compilation-buffer-p (car buffers)))
  786.                 (and other-buffer
  787.                      (eq (car buffers) (current-buffer)))))
  788.       (setq buffers (cdr buffers)))
  789.     (if buffers
  790.         (car buffers)
  791.       (or (and other-buffer
  792.            (compilation-buffer-p (current-buffer))
  793.            ;; The current buffer is a compilation buffer.
  794.            (progn
  795.              (if other-buffer
  796.              (message "This is the only compilation buffer."))
  797.              (current-buffer)))
  798.           (error "No compilation started!")))))))
  799.  
  800. ;;;###autoload
  801. (defun next-error (&optional argp)
  802.   "Visit next compilation error message and corresponding source code.
  803. This operates on the output from the \\[compile] command.
  804. If all preparsed error messages have been processed,
  805. the error message buffer is checked for new ones.
  806.  
  807. A prefix arg specifies how many error messages to move;
  808. negative means move back to previous error messages.
  809. Just C-u as a prefix means reparse the error message buffer
  810. and start at the first error.
  811.  
  812. \\[next-error] normally applies to the most recent compilation started,
  813. but as long as you are in the middle of parsing errors from one compilation
  814. output buffer, you stay with that compilation output buffer.
  815.  
  816. Use \\[next-error] in a compilation output buffer to switch to
  817. processing errors from that compilation.
  818.  
  819. See variables `compilation-parse-errors-function' and
  820. \`compilation-error-regexp-alist' for customization ideas."
  821.   (interactive "P")
  822.   (setq compilation-last-buffer (compilation-find-buffer))
  823.   (compilation-goto-locus (compilation-next-error-locus
  824.                ;; We want to pass a number here only if
  825.                ;; we got a numeric prefix arg, not just C-u.
  826.                (and (not (consp argp))
  827.                 (prefix-numeric-value argp))
  828.                (consp argp))))
  829. ;;;###autoload (define-key ctl-x-map "`" 'next-error)
  830.  
  831. (defun compilation-next-error-locus (&optional move reparse)
  832.   "Visit next compilation error and return locus in corresponding source code.
  833. This operates on the output from the \\[compile] command.
  834. If all preparsed error messages have been processed,
  835. the error message buffer is checked for new ones.
  836.  
  837. Returns a cons (ERROR . SOURCE) of two markers: ERROR is a marker at the
  838. location of the error message in the compilation buffer, and SOURCE is a
  839. marker at the location in the source code indicated by the error message.
  840.  
  841. Optional first arg MOVE says how many error messages to move forwards (or
  842. backwards, if negative); default is 1.  Optional second arg REPARSE, if
  843. non-nil, says to reparse the error message buffer and reset to the first
  844. error (plus MOVE - 1).
  845.  
  846. The current buffer should be the desired compilation output buffer."
  847.   (or move (setq move 1))
  848.   (compile-reinitialize-errors reparse nil (and (not reparse)
  849.                         (if (< move 1) 0 (1- move))))
  850.   (let (next-errors next-error)
  851.     (save-excursion
  852.       (set-buffer compilation-last-buffer)
  853.       ;; compilation-error-list points to the "current" error.
  854.       (setq next-errors 
  855.         (if (> move 0)
  856.         (nthcdr (1- move)
  857.             compilation-error-list)
  858.           ;; Zero or negative arg; we need to move back in the list.
  859.           (let ((n (1- move))
  860.             (i 0)
  861.             (e compilation-old-error-list))
  862.         ;; See how many cdrs away the current error is from the start.
  863.         (while (not (eq e compilation-error-list))
  864.           (setq i (1+ i)
  865.             e (cdr e)))
  866.         (if (> (- n) i)
  867.             (error "Moved back past first error")
  868.           (nthcdr (+ i n) compilation-old-error-list))))
  869.         next-error (car next-errors))
  870.       (while
  871.       (if (null next-error)
  872.           (progn
  873.         (and move (/= move 1)
  874.              (error (if (> move 0)
  875.                 "Moved past last error")
  876.                 "Moved back past first error"))
  877.         (compilation-forget-errors)
  878.         (error (concat compilation-error-message
  879.                    (and (get-buffer-process (current-buffer))
  880.                     (eq (process-status
  881.                      (get-buffer-process
  882.                       (current-buffer)))
  883.                     'run)
  884.                     " yet"))))
  885.         (setq compilation-error-list (cdr next-errors))
  886.         (if (null (cdr next-error))
  887.         ;; This error is boring.  Go to the next.
  888.         t
  889.           (or (markerp (cdr next-error))
  890.           ;; This error has a filename/lineno pair.
  891.           ;; Find the file and turn it into a marker.
  892.           (let* ((fileinfo (car (cdr next-error)))
  893.              (buffer (compilation-find-file (cdr fileinfo)
  894.                             (car fileinfo)
  895.                             (car next-error))))
  896.             (if (null buffer)
  897.             ;; We can't find this error's file.
  898.             ;; Remove all errors in the same file.
  899.             (progn
  900.               (setq next-errors compilation-old-error-list)
  901.               (while next-errors
  902.                 (and (consp (cdr (car next-errors)))
  903.                  (equal (car (cdr (car next-errors)))
  904.                     fileinfo)
  905.                  (progn
  906.                    (set-marker (car (car next-errors)) nil)
  907.                    (setcdr (car next-errors) nil)))
  908.                 (setq next-errors (cdr next-errors)))
  909.               ;; Look for the next error.
  910.               t)
  911.               ;; We found the file.  Get a marker for this error.
  912.               ;; compilation-old-error-list is a buffer-local
  913.               ;; variable, so we must be careful to extract its value
  914.               ;; before switching to the source file buffer.
  915.               (let ((errors compilation-old-error-list)
  916.                 (last-line (nth 1 (cdr next-error)))
  917.                 (column (nth 2 (cdr next-error))))
  918.             (set-buffer buffer)
  919.             (save-excursion
  920.               (save-restriction
  921.                 (widen)
  922.                 (goto-line last-line)
  923.                 (if column
  924.                 (move-to-column column)
  925.                   (beginning-of-line))
  926.                 (setcdr next-error (point-marker))
  927.                 ;; Make all the other error messages referring
  928.                 ;; to the same file have markers into the buffer.
  929.                 (while errors
  930.                   (and (consp (cdr (car errors)))
  931.                    (equal (car (cdr (car errors))) fileinfo)
  932.                    (let* ((this (nth 1 (cdr (car errors))))
  933.                       (column (nth 2 (cdr (car errors))))
  934.                       (lines (- this last-line)))
  935.                      (if (eq selective-display t)
  936.                      ;; When selective-display is t,
  937.                      ;; each C-m is a line boundary,
  938.                      ;; as well as each newline.
  939.                      (if (< lines 0)
  940.                          (re-search-backward "[\n\C-m]"
  941.                                  nil 'end
  942.                                  (- lines))
  943.                        (re-search-forward "[\n\C-m]"
  944.                                   nil 'end
  945.                                   lines))
  946.                        (forward-line lines))
  947.                      (if column
  948.                      (move-to-column column))
  949.                      (setq last-line this)
  950.                      (setcdr (car errors) (point-marker))))
  951.                   (setq errors (cdr errors)))))))))
  952.           ;; If we didn't get a marker for this error, or this
  953.           ;; marker's buffer was killed, go on to the next one.
  954.           (or (not (markerp (cdr next-error)))
  955.           (not (marker-buffer (cdr next-error))))))
  956.     (setq next-errors compilation-error-list
  957.           next-error (car next-errors))))
  958.  
  959.     ;; Skip over multiple error messages for the same source location,
  960.     ;; so the next C-x ` won't go to an error in the same place.
  961.     (while (and compilation-error-list
  962.         (equal (cdr (car compilation-error-list)) (cdr next-error)))
  963.       (setq compilation-error-list (cdr compilation-error-list)))
  964.  
  965.     ;; We now have a marker for the position of the error source code.
  966.     ;; NEXT-ERROR is a cons (ERROR . SOURCE) of two markers.
  967.     next-error))
  968.  
  969. (defun compilation-goto-locus (next-error)
  970.   "Jump to an error locus returned by `compilation-next-error-locus'.
  971. Takes one argument, a cons (ERROR . SOURCE) of two markers.
  972. Selects a window with point at SOURCE, with another window displaying ERROR."
  973.   (if (and (window-dedicated-p (selected-window))
  974.        (eq (selected-window) (frame-root-window)))
  975.       (switch-to-buffer-other-frame (marker-buffer (cdr next-error)))
  976.     (switch-to-buffer (marker-buffer (cdr next-error))))
  977.   (goto-char (cdr next-error))
  978.   ;; If narrowing got in the way of
  979.   ;; going to the right place, widen.
  980.   (or (= (point) (marker-position (cdr next-error)))
  981.       (progn
  982.     (widen)
  983.     (goto-char (cdr next-error))))
  984.  
  985.   ;; Show compilation buffer in other window, scrolled to this error.
  986.   (let* ((pop-up-windows t)
  987.      (w (display-buffer (marker-buffer (car next-error)))))
  988.     (set-window-point w (car next-error))
  989.     (set-window-start w (car next-error))))
  990.  
  991. ;; Find a buffer for file FILENAME.
  992. ;; Search the directories in compilation-search-path.
  993. ;; A nil in compilation-search-path means to try the
  994. ;; current directory, which is passed in DIR.
  995. ;; If FILENAME is not found at all, ask the user where to find it.
  996. ;; Pop up the buffer containing MARKER and scroll to MARKER if we ask the user.
  997. (defun compilation-find-file (filename dir marker)
  998.   (let ((dirs compilation-search-path)
  999.     result name)
  1000.     (while (and dirs (null result))
  1001.       (setq name (expand-file-name filename (or (car dirs) dir))
  1002.         result (and (file-exists-p name)
  1003.             (find-file-noselect name))
  1004.         dirs (cdr dirs)))
  1005.     (or result
  1006.     ;; The file doesn't exist.
  1007.     ;; Ask the user where to find it.
  1008.     ;; If he hits C-g, then the next time he does
  1009.     ;; next-error, he'll skip past it.
  1010.     (progn
  1011.       (let* ((pop-up-windows t)
  1012.          (w (display-buffer (marker-buffer marker))))
  1013.         (set-window-point w marker)
  1014.         (set-window-start w marker))
  1015.       (setq name
  1016.         (expand-file-name
  1017.          (read-file-name
  1018.           (format "Find this error in: (default %s) "
  1019.               filename) dir filename t)))
  1020.       (if (file-directory-p name)
  1021.           (setq name (concat (file-name-as-directory name) filename)))
  1022.       (if (file-exists-p name)
  1023.           (find-file-noselect name))))))
  1024.  
  1025. ;; Set compilation-error-list to nil, and unchain the markers that point to the
  1026. ;; error messages and their text, so that they no longer slow down gap motion.
  1027. ;; This would happen anyway at the next garbage collection, but it is better to
  1028. ;; do it right away.
  1029. (defun compilation-forget-errors ()
  1030.   (while compilation-old-error-list
  1031.     (let ((next-error (car compilation-old-error-list)))
  1032.       (set-marker (car next-error) nil)
  1033.       (if (markerp (cdr next-error))
  1034.       (set-marker (cdr next-error) nil)))
  1035.     (setq compilation-old-error-list (cdr compilation-old-error-list)))
  1036.   (setq compilation-error-list nil
  1037.     compilation-directory-stack nil
  1038.     compilation-parsing-end 1))
  1039.  
  1040.  
  1041. (defun count-regexp-groupings (regexp)
  1042.   "Return the number of \\( ... \\) groupings in REGEXP (a string)."
  1043.   (let ((groupings 0)
  1044.     (len (length regexp))
  1045.     (i 0)
  1046.     c)
  1047.     (while (< i len)
  1048.       (setq c (aref regexp i)
  1049.         i (1+ i))
  1050.       (cond ((= c ?\[)
  1051.          ;; Find the end of this [...].
  1052.          (while (and (< i len)
  1053.              (not (= (aref regexp i) ?\])))
  1054.            (setq i (1+ i))))
  1055.         ((= c ?\\)
  1056.          (if (< i len)
  1057.          (progn
  1058.            (setq c (aref regexp i)
  1059.              i (1+ i))
  1060.            (if (= c ?\))
  1061.                ;; We found the end of a grouping,
  1062.                ;; so bump our counter.
  1063.                (setq groupings (1+ groupings))))))))
  1064.     groupings))
  1065.  
  1066. (defun compilation-parse-errors (limit-search find-at-least)
  1067.   "Parse the current buffer as grep, cc or lint error messages.
  1068. See variable `compilation-parse-errors-function' for the interface it uses."
  1069.   (setq compilation-error-list nil)
  1070.   (message "Parsing error messages...")
  1071.   (let (text-buffer orig orig-expanded parent-expanded
  1072.     regexp enter-group leave-group error-group
  1073.     alist subexpr error-regexp-groups
  1074.     (found-desired nil)
  1075.     (compilation-num-errors-found 0))
  1076.  
  1077.     ;; Don't reparse messages already seen at last parse.
  1078.     (goto-char compilation-parsing-end)
  1079.     ;; Don't parse the first two lines as error messages.
  1080.     ;; This matters for grep.
  1081.     (if (bobp)
  1082.     (progn
  1083.       (forward-line 2)
  1084.       ;; Move back so point is before the newline.
  1085.       ;; This matters because some error regexps use \n instead of ^
  1086.       ;; to be faster.
  1087.       (forward-char -1)))
  1088.  
  1089.     ;; Compile all the regexps we want to search for into one.
  1090.     (setq regexp (concat "\\(" compilation-enter-directory-regexp "\\)\\|"
  1091.              "\\(" compilation-leave-directory-regexp "\\)\\|"
  1092.              "\\(" (mapconcat (function
  1093.                        (lambda (elt)
  1094.                          (concat "\\(" (car elt) "\\)")))
  1095.                       compilation-error-regexp-alist
  1096.                       "\\|") "\\)"))
  1097.  
  1098.     ;; Find out how many \(...\) groupings are in each of the regexps, and set
  1099.     ;; *-GROUP to the grouping containing each constituent regexp (whose
  1100.     ;; subgroups will come immediately thereafter) of the big regexp we have
  1101.     ;; just constructed.
  1102.     (setq enter-group 1
  1103.       leave-group (+ enter-group
  1104.              (count-regexp-groupings
  1105.               compilation-enter-directory-regexp)
  1106.              1)
  1107.       error-group (+ leave-group
  1108.              (count-regexp-groupings
  1109.               compilation-leave-directory-regexp)
  1110.              1))
  1111.  
  1112.     ;; Compile an alist (IDX FILE LINE [COL]), where IDX is the number of
  1113.     ;; the subexpression for an entire error-regexp, and FILE and LINE (and
  1114.     ;; possibly COL) are the numbers for the subexpressions giving the file
  1115.     ;; name and line number (and possibly column number).
  1116.     (setq alist (or compilation-error-regexp-alist
  1117.             (error "compilation-error-regexp-alist is empty!"))
  1118.       subexpr (1+ error-group))
  1119.     (while alist
  1120.       (setq error-regexp-groups
  1121.         (cons (list subexpr
  1122.             (+ subexpr (nth 1 (car alist)))
  1123.             (+ subexpr (nth 2 (car alist)))
  1124.             (and (nth 3 (car alist))
  1125.                  (+ subexpr (nth 3 (car alist)))))
  1126.           error-regexp-groups))
  1127.       (setq subexpr (+ subexpr 1 (count-regexp-groupings (car (car alist)))))
  1128.       (setq alist (cdr alist)))
  1129.  
  1130.     (setq orig default-directory)
  1131.     (setq orig-expanded (file-truename orig))
  1132.     (setq parent-expanded (expand-file-name "../" orig-expanded))
  1133.  
  1134.     (while (and (not found-desired)
  1135.         ;; We don't just pass LIMIT-SEARCH to re-search-forward
  1136.         ;; because we want to find matches containing LIMIT-SEARCH
  1137.         ;; but which extend past it.
  1138.         (re-search-forward regexp nil t))
  1139.  
  1140.       ;; Figure out which constituent regexp matched.
  1141.       (cond ((match-beginning enter-group)
  1142.          ;; The match was the enter-directory regexp.
  1143.          (let ((dir
  1144.             (file-name-as-directory
  1145.              (expand-file-name
  1146.               (buffer-substring (match-beginning (+ enter-group 1))
  1147.                     (match-end (+ enter-group 1)))))))
  1148.            ;; The directory name in the "entering" message
  1149.            ;; is a truename.  Try to convert it to a form
  1150.            ;; like what the user typed in.
  1151.            (setq dir
  1152.              (compile-abbreviate-directory dir orig orig-expanded
  1153.                            parent-expanded))
  1154.            (setq compilation-directory-stack
  1155.              (cons dir compilation-directory-stack))
  1156.            (and (file-directory-p dir)
  1157.             (setq default-directory dir)))
  1158.  
  1159.          (and limit-search (>= (point) limit-search)
  1160.           ;; The user wanted a specific error, and we're past it.
  1161.           ;; We do this check here (and in the leave-group case)
  1162.           ;; rather than at the end of the loop because if the last
  1163.           ;; thing seen is an error message, we must carefully
  1164.           ;; discard the last error when it is the first in a new
  1165.           ;; file (see below in the error-group case).
  1166.           (setq found-desired t)))
  1167.  
  1168.         ((match-beginning leave-group)
  1169.          ;; The match was the leave-directory regexp.
  1170.          (let ((beg (match-beginning (+ leave-group 1)))
  1171.            (stack compilation-directory-stack))
  1172.            (if beg
  1173.            (let ((dir
  1174.               (file-name-as-directory
  1175.                (expand-file-name
  1176.                 (buffer-substring beg
  1177.                           (match-end (+ leave-group
  1178.                                 1)))))))
  1179.              ;; The directory name in the "entering" message
  1180.              ;; is a truename.  Try to convert it to a form
  1181.              ;; like what the user typed in.
  1182.              (setq dir
  1183.                (compile-abbreviate-directory dir orig orig-expanded
  1184.                              parent-expanded))
  1185.              (while (and stack
  1186.                  (not (string-equal (car stack) dir)))
  1187.                (setq stack (cdr stack)))))
  1188.            (setq compilation-directory-stack (cdr stack))
  1189.            (setq stack (car compilation-directory-stack))
  1190.            (if stack
  1191.            (setq default-directory stack))
  1192.            )
  1193.  
  1194.          (and limit-search (>= (point) limit-search)
  1195.           ;; The user wanted a specific error, and we're past it.
  1196.           ;; We do this check here (and in the enter-group case)
  1197.           ;; rather than at the end of the loop because if the last
  1198.           ;; thing seen is an error message, we must carefully
  1199.           ;; discard the last error when it is the first in a new
  1200.           ;; file (see below in the error-group case).
  1201.           (setq found-desired t)))
  1202.  
  1203.         ((match-beginning error-group)
  1204.          ;; The match was the composite error regexp.
  1205.          ;; Find out which individual regexp matched.
  1206.          (setq alist error-regexp-groups)
  1207.          (while (and alist
  1208.              (null (match-beginning (car (car alist)))))
  1209.            (setq alist (cdr alist)))
  1210.          (if alist
  1211.          (setq alist (car alist))
  1212.            (error "compilation-parse-errors: impossible regexp match!"))
  1213.          
  1214.          ;; Extract the file name and line number from the error message.
  1215.          (let ((beginning-of-match (match-beginning 0)) ;looking-at nukes
  1216.            (filename (buffer-substring (match-beginning (nth 1 alist))
  1217.                            (match-end (nth 1 alist))))
  1218.            (linenum (string-to-int
  1219.                  (buffer-substring
  1220.                   (match-beginning (nth 2 alist))
  1221.                   (match-end (nth 2 alist)))))
  1222.            (column (and (nth 3 alist)
  1223.                 (string-to-int
  1224.                  (buffer-substring
  1225.                   (match-beginning (nth 3 alist))
  1226.                   (match-end (nth 3 alist)))))))
  1227.  
  1228.            ;; Check for a comint-file-name-prefix and prepend it if
  1229.            ;; appropriate.  (This is very useful for
  1230.            ;; compilation-minor-mode in an rlogin-mode buffer.)
  1231.            (and (boundp 'comint-file-name-prefix)
  1232.             ;; If the file name is relative, default-directory will
  1233.             ;; already contain the comint-file-name-prefix (done by
  1234.             ;; compile-abbreviate-directory).
  1235.             (file-name-absolute-p filename)
  1236.             (setq filename (concat comint-file-name-prefix filename)))
  1237.            (setq filename (cons default-directory filename))
  1238.  
  1239.            ;; Locate the erring file and line.
  1240.            ;; Cons a new elt onto compilation-error-list,
  1241.            ;; giving a marker for the current compilation buffer
  1242.            ;; location, and the file and line number of the error.
  1243.            (save-excursion
  1244.          (beginning-of-line 1)
  1245.          (let ((this (cons (point-marker)
  1246.                    (list filename linenum column))))
  1247.            ;; Don't add the same source line more than once.
  1248.            (if (equal (cdr this) (cdr (car compilation-error-list)))
  1249.                nil
  1250.              (setq compilation-error-list
  1251.                (cons this
  1252.                  compilation-error-list))
  1253.              (setq compilation-num-errors-found
  1254.                (1+ compilation-num-errors-found)))))
  1255.            (and (or (and find-at-least (> compilation-num-errors-found
  1256.                           find-at-least))
  1257.             (and limit-search (>= (point) limit-search)))
  1258.             ;; We have found as many new errors as the user wants,
  1259.             ;; or past the buffer position he indicated.  We
  1260.             ;; continue to parse until we have seen all the
  1261.             ;; consecutive errors in the same file, so the error
  1262.             ;; positions will be recorded as markers in this buffer
  1263.             ;; that might change.
  1264.             (cdr compilation-error-list) ; Must check at least two.
  1265.             (not (equal (car (cdr (nth 0 compilation-error-list)))
  1266.                 (car (cdr (nth 1 compilation-error-list)))))
  1267.             (progn
  1268.               ;; Discard the error just parsed, so that the next
  1269.               ;; parsing run can get it and the following errors in
  1270.               ;; the same file all at once.  If we didn't do this, we
  1271.               ;; would have the same problem we are trying to avoid
  1272.               ;; with the test above, just delayed until the next run!
  1273.               (setq compilation-error-list
  1274.                 (cdr compilation-error-list))
  1275.               (goto-char beginning-of-match)
  1276.               (setq found-desired t)))
  1277.            )
  1278.          )
  1279.         (t
  1280.          (error "compilation-parse-errors: known groups didn't match!")))
  1281.  
  1282.       (message "Parsing error messages...%d (%.0f%% of buffer)"
  1283.            compilation-num-errors-found
  1284.            ;; Use floating-point because (* 100 (point)) frequently
  1285.            ;; exceeds the range of Emacs Lisp integers.
  1286.            (/ (* 100.0 (point)) (point-max)))
  1287.  
  1288.       (and limit-search (>= (point) limit-search)
  1289.        ;; The user wanted a specific error, and we're past it.
  1290.        (setq found-desired t)))
  1291.     (setq compilation-parsing-end (if found-desired
  1292.                       (point)
  1293.                     ;; We have searched the whole buffer.
  1294.                     (point-max))))
  1295.   (setq compilation-error-list (nreverse compilation-error-list))
  1296.   (message "Parsing error messages...done"))
  1297.  
  1298. ;; If directory DIR is a subdir of ORIG or of ORIG's parent,
  1299. ;; return a relative name for it starting from ORIG or its parent.
  1300. ;; ORIG-EXPANDED is an expanded version of ORIG.
  1301. ;; PARENT-EXPANDED is an expanded version of ORIG's parent.
  1302. ;; Those two args could be computed here, but we run faster by
  1303. ;; having the caller compute them just once.
  1304. (defun compile-abbreviate-directory (dir orig orig-expanded parent-expanded)
  1305.   ;; Check for a comint-file-name-prefix and prepend it if appropriate.
  1306.   ;; (This is very useful for compilation-minor-mode in an rlogin-mode
  1307.   ;; buffer.)
  1308.   (if (boundp 'comint-file-name-prefix)
  1309.       (setq dir (concat comint-file-name-prefix dir)))
  1310.  
  1311.   (if (and (> (length dir) (length orig-expanded))
  1312.        (string= orig-expanded
  1313.             (substring dir 0 (length orig-expanded))))
  1314.       (setq dir
  1315.         (concat orig
  1316.             (substring dir (length orig-expanded)))))
  1317.   (if (and (> (length dir) (length parent-expanded))
  1318.        (string= parent-expanded
  1319.             (substring dir 0 (length parent-expanded))))
  1320.     (setq dir
  1321.       (concat (file-name-directory
  1322.            (directory-file-name orig))
  1323.           (substring dir (length parent-expanded)))))
  1324.   dir)
  1325.  
  1326. (provide 'compile)
  1327.  
  1328. ;;; compile.el ends here
  1329.